<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:t="http://myfaces.apache.org/tomahawk" 
      xmlns:ui="http://java.sun.com/jsf/facelets" >
    <h:head>
        <title>Movimento de Ordem de Serviço</title>
    </h:head>
    <h:body>
        <t:saveState value="#{osBean.ordemServico}" />
        <t:saveState value="#{osBean.servico}" />
        <t:saveState value="#{osBean.servicos}" />
        <t:saveState value="#{osBean.clientes}" />
        <h:form id="formOs" >
            <p:panel header="Ordem de Serviço" >
                <h:panelGrid id="panelGrid" columns="3" >
                    <h:outputLabel value="Data: *" />
                    <p:calendar id="dtOrdemServico" value="#{osBean.ordemServico.dtOrdemServico}" required="true" requiredMessage="Campo obrigatório" timeZone="America/Sao_Paulo" pattern="dd/MM/yyyy" />
                    <p:message for="dtOrdemServico" />
                    <h:outputLabel value="Cliente: *" />
                    <h:panelGroup>
                        <p:inputText id="cliente" value="#{osBean.ordemServico.cliente.nome}" disabled="true" />
                        <p:commandButton id="btnSelecionarCliente" value="Selecionar" oncomplete="dialogCliente.show()" />
                    </h:panelGroup>
                    <p:message for="clienteValida" />
                    <h:outputLabel value="Funcionário: *" />
                    <p:inputText id="funcionario" value="#{osBean.ordemServico.funcionario.nome}" disabled="true" />
                    <p:message for="funcionario" />
                    <h:outputLabel value="Valor: *" />
                    <p:inputText id="valor" value="#{osBean.ordemServico.valor}" converter="converterBigDecimal" disabled="true" />
                    <p:message for="valor" />
                    <h:outputLabel value="Descrição: *" />
                    <p:inputTextarea id="descricao" value="#{osBean.ordemServico.descricao}" />
                    <p:message for="descricao" />

                    <h:inputHidden id="clienteValida" value="cliente" validator="#{osBean.validatorCliente}" />
                    <h:inputHidden id="servicosValida" value="servicos" validator="#{osBean.validatorServicos}" />
                </h:panelGrid>

                <p:fieldset id="fieldset" legend="Serviços" >
                    <p:outputPanel id="panelServico" >
                        <h:inputHidden id="servico" value="servico" validator="#{osBean.validatorServico}" />
                        <p:message for="servico" />
                        <p:message for="servicosValida" />
                        <h:outputLabel value="Serviço: *" />
                        <p:inputText id="nome" value="#{osBean.servico.nome}" disabled="true" />
                        <h:outputLabel value="Valor: *" />
                        <p:inputText id="valorServico" value="#{osBean.servico.valor}" disabled="true" />
                        <p:commandButton id="btnSelecionarServico" value="Selecionar" oncomplete="dialogServico.show()" />
                        <p:commandButton id="btnAdicionar" value="Adicionar" actionListener="#{osBean.adicionarServico}" update="formOs" process="panelServico" />
                    </p:outputPanel>
                    <p:dataTable id="tabela" value="#{osBean.ordemServico.itensOrdemServico}" var="i" emptyMessage="Sem serviços adicionados" >
                        <p:column headerText="Serviço" >
                            <h:outputText value="#{i.servico.nome}" />
                        </p:column>
                        <p:column headerText="Valor" >
                            <h:outputText value="#{i.valor}" />
                        </p:column>
                        <p:column headerText="Excluir" >
                            <p:commandButton id="btnRemover" value="Remover" actionListener="#{osBean.removerServico}" update="formOs" process="@this" >
                                <f:attribute name="itensOrdemServico" value="#{i}" />
                            </p:commandButton>
                        </p:column>
                    </p:dataTable>
                </p:fieldset> 

                <p:commandButton id="btnSalvar" value="Salvar" actionListener="#{osBean.salvar}" update="formOs" process="@this, panelGrid" />
                <p:commandButton id="btnCancelar" value="Cancelar" action="/movimentos/osLista.jsf" immediate="true" ajax="false" />
            </p:panel>
        </h:form>

        <p:dialog id="dialogServico" widgetVar="dialogServico" header="Selecionar Serviço" appendToBody="true" height="600" width="800" >
            <h:form id="formDialogServico" >
                <h:outputLabel value="Serviço: " />
                <p:inputText value="#{osBean.nomeServico}" />
                <p:commandButton value="Consultar" actionListener="#{osBean.consultarServicos}" update="tabela" />

                <p:dataTable id="tabela" value="#{osBean.servicos}" var="s" emptyMessage="Sem resultados" >
                    <p:column headerText="Serviço" >
                        <h:outputText value="#{s.nome}" />
                    </p:column>
                    <p:column headerText="Valor" >
                        <h:outputText value="#{s.valor}" converter="converterBigDecimal" />
                    </p:column>
                    <p:column headerText="Selecionar" >
                        <p:commandButton id="btnSelecionar" value="X" oncomplete="dialogServico.hide()" update="formOs:panelServico" >
                            <f:setPropertyActionListener value="#{s}" target="#{osBean.servico}" />
                        </p:commandButton>
                    </p:column>
                </p:dataTable>
            </h:form>
        </p:dialog>

        <p:dialog id="dialogCliente" widgetVar="dialogCliente" header="Selecionar Cliente" appendToBody="true" height="600" width="800" >
            <h:form id="formDialogCliente" >
                <h:outputLabel value="Cliente: " />
                <p:inputText value="#{osBean.nomeCliente}" />
                <p:commandButton value="Consultar" actionListener="#{osBean.consultarClientes}" update="tabela" />

                <p:dataTable id="tabela" value="#{osBean.clientes}" var="c" emptyMessage="Sem resultados" >
                    <p:column headerText="Serviço" >
                        <h:outputText value="#{c.nome}" />
                    </p:column>
                    <p:column headerText="Valor" >
                        <h:outputText value="#{c.cnpj}" converter="converterCnpjCpf" />
                    </p:column>
                    <p:column headerText="Selecionar" >
                        <p:commandButton id="btnSelecionar" value="X" oncomplete="dialogCliente.hide()" update="formOs:cliente" >
                            <f:setPropertyActionListener value="#{c}" target="#{osBean.ordemServico.cliente}" />
                        </p:commandButton>
                    </p:column>
                </p:dataTable>
            </h:form>
        </p:dialog>
    </h:body>
</html>
